//
//  YBNotificationMessageViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/11/29.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBNotificationMessageViewController.h"
#import "GTPageController.h"
#import "YBMessageAcceptedViewController.h"
#import "YBMessageReleasedViewController.h"

@interface YBNotificationMessageViewController ()
@property (strong, nonatomic) GTPageController  *gtPageVc;
@property (nonatomic , strong) UILabel * numberLabel;
@end

@implementation YBNotificationMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
    if ([ZJUSERINFORMODEL.ORG_ACCOUNT.ISWORK integerValue] != 0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadUnReadViewWithNotification:) name:@"AcceptMessageUnRead" object:nil];
    }
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)reloadUnReadViewWithNotification:(NSNotification *)notification{
    if ([notification.userInfo[@"number"] integerValue] == 0) {
        self.numberLabel.hidden = YES;
    }
    else{
        self.numberLabel.hidden = NO;
        if ([notification.userInfo[@"number"] integerValue] > 99) {
            self.numberLabel.text = @"99+";
        }
        else{
            self.numberLabel.text = [NSString stringWithFormat:@"%@" , notification.userInfo[@"number"]];
        }
    }
}


#pragma mark - 界面
- (void)layoutUI{
    if ([ZJUSERINFORMODEL.ORG_ACCOUNT.ISWORK integerValue] == 0) {
        YBMessageAcceptedViewController * acceptVC = [YBMessageAcceptedViewController new];
        acceptVC.isNotAdmin = YES;
        [self.view addSubview:acceptVC.view];
        [self addChildViewController:acceptVC];
    }
    else{
        self.gtPageVc = [self getDefaultController];
        self.gtPageVc.view.frame = CGRectMake(0, navHeight , self.view.width, [UIScreen zj_screenHeight] - navHeight );
        [self.view addSubview:self.gtPageVc.view];
        [self addChildViewController:self.gtPageVc];
        [self.view addSubview:self.numberLabel];
    }
    
    
}



#pragma mark - 私有方法
- (GTPageController *)getDefaultController{
    
    YBMessageAcceptedViewController * acceptVC = [YBMessageAcceptedViewController new];
    YBMessageReleasedViewController * releaseVC = [YBMessageReleasedViewController new];
    NSArray *titles = @[ @"已接收", @"已发布"];
    
    
    NSArray *viewVcs = @[[acceptVC class],[releaseVC class]];
    
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat width = [NSString sizeOfString:obj font:16 * [UIScreen zj_textFontScale]  width:self.view.width].width + 12 * [UIScreen zj_screenWScale];
        [titlesWidth addObject:@(width)];
    }];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewVcs andTheirTitles:titles];
    pageVC.pageAnimatable = YES;
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.menuBGColor = [UIColor whiteColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.progressColor = [UIColor themeColor];
    pageVC.titleColorSelected = [UIColor themeColor];
    pageVC.titleColorNormal = [UIColor textBlackColor];
    pageVC.titleSizeNormal = 16 * [UIScreen zj_textFontScale];
    pageVC.titleSizeSelected = 17 * [UIScreen zj_textFontScale];
    pageVC.menuHeight = 50 * [UIScreen zj_screenHScale];
    return pageVC;
}



-(UILabel *)numberLabel
{
    if (!_numberLabel) {
        _numberLabel = [ToolClass creatLabelWithFrame:CGRectMake([UIScreen zj_screenWith] / 2 - ItemWidth * 70, ItemHeight * 28 + navHeight, [UIScreen zj_screenWScale] * 22, [UIScreen zj_screenWScale] * 22) font:10 textColor:[UIColor whiteColor] text:@""];
        _numberLabel.backgroundColor = [UIColor themeColor];
        _numberLabel.clipsToBounds = YES;
        _numberLabel.layer.cornerRadius= [UIScreen zj_screenWScale] * 11;
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        _numberLabel.hidden = YES;
    }
    return _numberLabel;
}

@end
